package com.shengbo.product.service.impl;

import com.shengbo.product.mapper.ProductMapper;
import com.shengbo.product.service.ProductService;
import com.shengbo.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import com.alibaba.fastjson.JSON;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Override
    @Cacheable(cacheNames="product", key="#attrId", sync=true)
    public AttrVo getAttrVo(int attrId) {
        AttrVo attrVo = productMapper.getAttrVo(attrId);
        return attrVo;
    }


    @Override
    @CacheEvict(cacheNames="product", key="#attrId")
    public int deleteAttrVo(int attrId) {
        BoundHashOperations<String, Object, Object> productOps = stringRedisTemplate.boundHashOps("product");
        String s = (String) productOps.get(String.valueOf(attrId));
        AttrVo attrVo = JSON.parseObject(s, AttrVo.class);
        System.out.println(attrVo);
        return productMapper.deleteAttrVo(attrId);
    }

    @Override
    public int insertAttrVo(AttrVo attrVo) {
        return productMapper.insertAttrVo(attrVo);
    }

    @Override
    public int updateAttrVo(AttrVo attrVo) {
        return productMapper.updateAttrVo(attrVo);
    }
}
